#pragma once
#include "Queue.h"

class BitmapFrame
{
public:
	int FrameIndex;
	HBITMAP Frame;

	BitmapFrame(int idx, HBITMAP hBmp)
		: FrameIndex(idx),
		Frame(hBmp)
	{
	}
	~BitmapFrame()
	{
		//if(Frame)
		//	::DeleteObject(Frame);
	}
};

// CRetrieverMaster

class CRetrieverMaster : public CWinThread
{
	DECLARE_DYNCREATE(CRetrieverMaster)

	CQueue<BitmapFrame*> m_qBitmaps;
	CQueue<CWinThread*> m_qSlaves;
	BOOL	m_bEnd;
	HWND	m_hwndNotifier;
	INT		m_iSlaveCount;
	INT		m_iEndedSlaves;
	BOOL	m_bReceivedEndSignal;

protected:
	CRetrieverMaster();           // protected constructor used by dynamic creation
	virtual ~CRetrieverMaster();

	DECLARE_MESSAGE_MAP()

public:

	// was sent to master
	static const UINT WM_MASTERMESSAGE = WM_USER + 10;
	enum MasterWParam
	{
		MasterWParamNewBitmapArrived = 0,
		MasterWParamNewBitmapEnded = 1,
		MasterWParamJobRequested = 10,
		MasterWParamJobDone = 11,
		MasterWParamSlaveEnded = 20
	};

	// was sent to slaves
	static const UINT WM_SLAVEMESSAGE = WM_USER + 11;
	enum SlaveWParam
	{
		SlaveWParamNewBitmapArrived = 0,
		SlaveWParamJobEnded = 1
	};

	// was sent by master to Notifier
	static const UINT WM_NOTIFIERMESSAGE = WM_USER + 21;
	enum NotifierWParam
	{
		NotifierWParamBitmapProcessed = 0
	};

	virtual BOOL InitInstance();
	virtual int ExitInstance();

	void SetNotifier(HWND hWnd);
	void SetSlaveCount(INT iCount);
	void DelieverFrames(CWinThread* threadSlave, BitmapFrame* frame);

	void OnWmMasterMessage(WPARAM wParam, LPARAM lParam);
	virtual int Run();
};


